'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFWsCtrl
 * @description
 * # FinanceFWsCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('FinanceFWsCtrl', function ($scope, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
    var id = $scope.process_id = $routeParams.id
    $scope.choosePeople = function () {
      var relModuleObj = {
        name: "ManageBidchargeCtrl",
        selectTab: "project_money_select",
        selectTabContent: "工程名称"
      };
      Util.savaParamForReview($scope.param, id);
      Util.savaDataforsearch(relModuleObj);
      $location.path("finance/choosepeople")
    };
    // 选择报销单明细
    $scope.showCertificate = function () {
      var param = {};
      param.type = "税种";
      Util.getKeyValue(param, function (data) {
        $scope.resiTypeList = data.data
        $rootScope.$emit('showmasker');
        $scope.showCerPicker = true;
      })
    };
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.index = -1;
    };
    $scope.addCertificate = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      };
      $scope.param.details.push($scope.picker);
      // 隐藏表单
      hideCertificate();
    };
    var hideCertificate = function () {
      $scope.picker = {};
      $rootScope.$emit('closemasker');
      $scope.showCerPicker = false;
    };
    $scope.$on('closemasker', function () {
      hideCertificate()
    });
    $scope.cacelCertificate = function () {
      hideCertificate();
    };
    $scope.swipe = function (index) {
      $scope.index = index;
    }
    $scope.resetlist = function (index) {
      $scope.index = -1;
    }

    // 验证是否输入完整
    var checkPicker = function (data) {
      if (!data.type) {
        Util.Toast("请选择税种");
        return;
      }
      if (!data.rate) {
        Util.Toast("请填写税率");
        return;
      }
      if (!data.amount) {
        Util.Toast("请填写税额");
        return;
      }
      return true;
    };
    $scope.submit = function (operationId) {
      $scope.param.taxDetails = angular.toJson($scope.param.details)
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.taxrecord;
      options.param = angular.copy($scope.param, {});
      delete options.param.details
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择工程名称");
        return;
      }
      if (!$scope.param.taxNo) {
        Util.Toast("请输入税票编号");
        return;
      }
      if (!$scope.param.openDate) {
        Util.Toast("请选择填发日期");
        return;
      }
      if (!$scope.param.taxingAuthority) {
        Util.Toast("请输入税务机关");
        return;
      }
      if (!$scope.param.taxAmount) {
        if ($scope.param.taxAmount !== 0) {
          Util.Toast("请输入税票金额");
          return;
        }
      }
      if ($scope.param.details.length < 1) {
        Util.Toast("请添加税额明细");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    }
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        $scope.param.details = $scope.param.details || [];
      });
    };
    // 初始化
    init();
  });